package cn.yhbk.web.modules.manager.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import cn.yhbk.web.modules.manager.Mapper.SysMapper;
import cn.yhbk.web.modules.manager.Pojo.DTO.Sys.SysDto;
import cn.yhbk.web.modules.manager.Pojo.Entity.SysEntity;
import cn.yhbk.web.modules.manager.Pojo.VO.Sys.SysUpdate;
import cn.yhbk.web.modules.manager.Service.SysService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysServiceImpl extends ServiceImpl<SysMapper, SysEntity> implements SysService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public List<SysDto> getSysList() {
        String sysStr = redisTemplate.opsForValue().get("sysList");
        if (sysStr != null){
            return JSONUtil.toList(sysStr, SysDto.class);
        }
        List<SysDto> sysList = this.baseMapper.getSysList();
        redisTemplate.opsForValue().set("sysList", JSONUtil.toJsonStr(sysList));
        return this.baseMapper.getSysList();
    }

    @Override
    public void updateSys(SysUpdate sysUpdateList) {
        SysEntity sysEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<SysEntity>()
                .eq(SysEntity::getSysKey, sysUpdateList.getSysKey()));
        BeanUtils.copyProperties(sysUpdateList, sysEntity);
        this.baseMapper.updateById(sysEntity);
    }

    @Override
    public void addSys(SysUpdate sysUpdate) {
        SysEntity sysEntity = BeanUtil.copyProperties(sysUpdate, SysEntity.class);
        this.baseMapper.insert(sysEntity);
    }
}
